package com.ado.bazinga.core.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ado.bazinga.core.annotation.AdminLoginToken;
import com.ado.bazinga.core.annotation.ApiDesc;
import com.ado.bazinga.core.common.http.HttpResult;
import com.ado.bazinga.core.dal.entity.Good;
import com.ado.bazinga.core.service.GoodService;
import com.ado.bazinga.core.utils.FileUploadUtils;

/**
 *@author adonais
 *@date 2020年4月8日
 *@description UploadController.java
**/
@RestController
@RequestMapping(value = "/api/upload")
public class UploadController {
	
	@Value("${file.location.path}")
	private String fileLocation;
	@Autowired
	@Qualifier(value = "goodService")
	private GoodService goodService ;
	
	
	@ApiDesc(desc = "上传商品图片接口")
	@AdminLoginToken
	@PostMapping(value = "/uploadGoodImg")
	public HttpResult uploadGoodImg(String goodId , MultipartFile file) {
		String uri = FileUploadUtils.upload(file,"/upload",fileLocation);
		Good good = goodService.getGoodById(Integer.valueOf(goodId)) ;
		good.setPic(uri);
		boolean flag = goodService.updateGood(good) ;
		if (flag) {
			return HttpResult.ok("图片上传成功");
		}
		return HttpResult.error("图片上传失败") ;
	}

}
